package com.yixun.qfbao.controller;

import com.yixun.qfbao.api.service.DataPermissionService;
import com.yixun.qfbao.apidto.DeptPermissionBindDto;
import com.yixun.qfbao.apidto.SetDeptPermissionReqDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.hander.AuthorizeRoleHander;
import com.yixun.qfbao.hander.SetRoleCasememberHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.DeptPermissionDto;
import com.yixun.qfbao.request.BaseRequest;
import com.yixun.qfbao.request.GenericIdRequest;
import com.yixun.qfbao.request.datapermission.GetBindDeptPermissionReqDto;
import com.yixun.qfbao.response.BaseResponse;
import com.yixun.qfbao.utils.JSONUtil;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.AuthorizeRoleReqVo;
import com.yixun.qfbao.vo.SetRoleCaseMemberReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Controller
@ResponseBody
@RequestMapping("/api/dp")
public class DataPermissionController {
	
	private static final Logger logger = LoggerFactory.getLogger(DataPermissionController.class);

	@Autowired
	private LoginUtils loginUtils;
	@Autowired
	private AuthorizeRoleHander authorizeRoleHander;
	@Autowired
	private SetRoleCasememberHander setRoleCasememberHander;
	@Autowired
	private DataPermissionService dataPermissionService;

	/**
	 * 设置角色菜单、功能权限
	 */
	@PostMapping(value = "/authorizeRole", produces = "application/json")
	public String authorizeRole(@RequestBody AuthorizeRoleReqVo authorizeRoleReqVo) {
		try {
			return JSONUtil.toJSONStringWithDateFormat(authorizeRoleHander.hander(authorizeRoleReqVo));
		} catch (Exception e) {
			logger.error("authorizeRole error: {}", e);
		}
		return null;
	}

	/**
	 * 设置角色组织机构权限
	 */
	@PostMapping(value = "/setRoleCaseMember", produces = "application/json")
	public String setRoleCaseMember(@RequestBody SetRoleCaseMemberReqVo setRoleCaseMemberReqVo) {
		try {
			return JSONUtil.toJSONStringWithDateFormat(setRoleCasememberHander.hander(setRoleCaseMemberReqVo));
		} catch (Exception e) {
			logger.error("authorizeRole error: {}", e);
		}
		return null;
	}


	@ApiOperation(notes = "机构数据权限绑定情况", value="机构数据权限绑定情况")
	@PostMapping(value = "/getBindDeptPermission")
	public BaseResponse<DeptPermissionBindDto> getBindDeptPermission(@RequestBody BaseRequestDTO<GetBindDeptPermissionReqDto> request){
		BaseResponse<DeptPermissionBindDto> response = new BaseResponse<>();

		PartnerInfo partnerInfo = loginUtils.getUser(request.getParams().getUserId());
		if(Objects.isNull(partnerInfo) || Objects.isNull(partnerInfo.getHigherid())){
			return response;
		}

		DeptPermissionDto deptPermissionDto = new DeptPermissionDto();
		deptPermissionDto.setBelongsId(request.getParams().getPartnerId());
		deptPermissionDto.setCompanyId(partnerInfo.getHigherid());
		DeptPermissionBindDto bindDto = dataPermissionService.getBindDeptPermission(deptPermissionDto);
		response.setData(bindDto);
		response.setCode(ResultEnum.SUCCESS.getNo());
		response.setMsg(ResultEnum.SUCCESS.getDes());
		return response;
	}

	@ApiOperation(notes = "设置机构数据权限", value="设置机构数据权限")
	@PostMapping(value = "/setDeptPermission")
	public BaseResponse<String> setDeptPermission(@RequestBody BaseRequestDTO<SetDeptPermissionReqDto> request){
		BaseResponse<String> response = new BaseResponse<>();
		PartnerInfo partnerInfo = loginUtils.getUser(request.getParams().getUserId());
		if(Objects.isNull(partnerInfo) || Objects.isNull(partnerInfo.getHigherid())){
			response.setCode(ResultEnum.FAIL.getNo());
			response.setMsg(ResultEnum.FAIL.getDes());
			return response;
		}
		request.getParams().setMerchantId(partnerInfo.getHigherid());
		dataPermissionService.setDeptPermission(request.getParams());
		response.setCode(ResultEnum.SUCCESS.getNo());
		response.setMsg(ResultEnum.SUCCESS.getDes());
		return response;
	}

	@ApiOperation(notes = "重置机构数据权限", value="重置机构数据权限")
	@PostMapping(value = "/resetDeptPermission")
	public BaseResponse<String> resetDeptPermission(@RequestBody BaseRequestDTO<SetDeptPermissionReqDto> request){
		BaseResponse<String> response = new BaseResponse<>();
		if(Objects.isNull(request.getParams().getBelongsId())){
			response.setCode(ResultEnum.FAIL.getNo());
			response.setMsg(ResultEnum.FAIL.getDes());
			return response;
		}
		dataPermissionService.resetDeptPermission(request.getParams().getBelongsId());
		response.setCode(ResultEnum.SUCCESS.getNo());
		response.setMsg(ResultEnum.SUCCESS.getDes());
		return response;
	}

	@ApiOperation(notes = "设置机构数据权限", value="设置机构数据权限")
	@PostMapping(value = "/setAgencyDeptPermission")
	public BaseResponse<String> setAgencyDeptPermission(@RequestBody BaseRequestDTO<SetDeptPermissionReqDto> request){
		BaseResponse<String> response = new BaseResponse<>();
		PartnerInfo partnerInfo = loginUtils.getUser(request.getParams().getUserId());
		if(Objects.isNull(partnerInfo) || Objects.isNull(partnerInfo.getHigherid())){
			response.setCode(ResultEnum.FAIL.getNo());
			response.setMsg(ResultEnum.FAIL.getDes());
			return response;
		}
		request.getParams().setMerchantId(partnerInfo.getHigherid());
		dataPermissionService.setAgencyDeptPermission(request.getParams());
		response.setCode(ResultEnum.SUCCESS.getNo());
		response.setMsg(ResultEnum.SUCCESS.getDes());
		return response;
	}

}
